#include "misc.h"

GdkPixbuf * get_face_gdkpixbuf(int i) {
  int a = (i/3)+1;
  int b = (i%3)+1;
  char *image = g_strdup_printf("qq_%d-%d.png", a,b);
  gchar *filename = g_build_filename
		(DATADIR, "pixmaps", "gaim", "status", "default", image, NULL);
  g_free(image);
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename,NULL);
  g_free(filename);
  return pixbuf;
}

struct contact_info *qq_duplicate_info(struct contact_info *info) {
  struct contact_info *dup = g_new0(struct contact_info, 1);
  int i;
  gchar **info_field = (gchar **)info;
  gchar **dup_field = (gchar**) dup;
  
  for(i=0; i<(QQ_CONTACT_FIELDS); i++)
    dup_field[i] = g_strdup(info_field[i]);

  return dup;
}

void qq_read_packet_b
(uint8 *buf, uint8 **cursor, int buflen,uint8 *b) {
  if (*cursor <= buf + buflen - sizeof(*b)) {
    *b = **(uint8 **) cursor;
    *cursor += sizeof(*b);
  } else *b = 0;
}

void qq_read_packet_w
(uint8 *buf, uint8 **cursor, int buflen,uint16 *w) {
  if (*cursor <= buf + buflen - sizeof(*w)) {
    *w = ntohs(**(uint16 **) cursor);
    *cursor += sizeof(*w);
  } else *w = 0;
}

void qq_read_packet_dw
(uint8 *buf, uint8 **cursor, int buflen,uint32 *dw) {
  if (*cursor <= buf + buflen - sizeof(*dw)) {
    *dw = ntohl(**(uint32 **) cursor);
    *cursor += sizeof(*dw);
  } else *dw = 0;
}   

void qq_read_packet_data
(uint8 *buf, uint8 **cursor, int buflen, uint8 *data, int datalen) {
  if (*cursor <= buf + buflen - datalen) {
	   memcpy(data, *cursor, datalen);
		*cursor += datalen;
}}

void qq_create_packet_b
(uint8 *buf, uint8 **cursor, uint8 b) {
	if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(uint8)) {
    ** (uint8 **) cursor = b;
		*cursor += sizeof(uint8);
}}

void qq_create_packet_w
(uint8 *buf, uint8 **cursor, uint16 w) {
  if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(uint16)) {
    ** (uint16 **) cursor = htons(w);
    *cursor += sizeof(uint16);
}}

void qq_create_packet_dw
(uint8 *buf, uint8 **cursor, uint32 dw) {
  if (*cursor <= buf + MAX_PACKET_SIZE - sizeof(uint32)) {
    ** (uint32 **) cursor = htonl(dw);
    *cursor += sizeof(uint32);
}}

void qq_create_packet_data
(uint8 *buf, uint8 **cursor, const uint8 *data, int datalen) {
	if (*cursor <= buf + MAX_PACKET_SIZE - datalen) {         
    memcpy(*cursor, data, datalen);                         
    *cursor += datalen;                                     
}}


